package de.hauschild.jchronology.data1;

import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import de.hauschild.jchronology.DateUtils;

public class Chronology {

  private final Map<Date, ChronologyEntry> entrys;

  public Chronology() {
    this(new HashMap<Date, ChronologyEntry>());
  }

  public Chronology(Map<Date, ChronologyEntry> theEntrys) {
    entrys = theEntrys;
  }

  public void clean() {
    for (final Entry<Date, ChronologyEntry> entry : new HashSet<Entry<Date, ChronologyEntry>>(entrys.entrySet())) {
      if (entry.getValue().isEmpty()) {
        entrys.remove(entry.getKey());
      }
    }
  }

  public Set<Entry<Date, ChronologyEntry>> entrySet() {
    return entrys.entrySet();
  }

  public ChronologyEntry getEntry(Date theDate) {
    final Date date = DateUtils.truncate(theDate);
    ChronologyEntry entry = entrys.get(date);
    if (entry == null) {
      entry = new ChronologyEntry();
      entrys.put(date, entry);
    }
    return entry;
  }
}
